<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1>购物车加减例子</h1>
    <button id="addBtn">+</button>
    <!-- <div>123</div> -->
    <span id="text">0</span>
    <button id="subBtn">-</button>

    <script>
      // DOM(文档对象) BOM(Browser)
      let addBtnEle = document.getElementById('addBtn');
      let subBtnEle = document.getElementById('subBtn');
      let num = 2;
      //   console.log(addBtnEle);
      document.getElementById('text').innerHTML = num;
      addBtnEle.addEventListener('click', function () {
        // alert('点击了+');
        num = num + 1;
        document.getElementById('text').innerHTML = num;
      });
      subBtnEle.addEventListener('click', function () {
        num--;
        // --num
        // num = num -1;
        document.getElementById('text').innerHTML = num;
      });
    </script>
  </body>
</html>
